﻿@model IEnumerable<Db8.Data.Keyword>
@{
    ViewBag.Title = "Tags";
}

<section class="content">
<div>@Html.Partial("_Navigation")</div>

<table>
    <tr>
        <th>Tag</th>
        <th>#</th>
        <th>Bulk Update</th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>@Html.ActionLink(item.Value, "Index", "Claim", new { filter = "\"" + item.Value + "\"" }, null)</td>
        <td>@Html.DisplayFor(modelItem => item.ClaimCount)</td>
        <td>@Html.TextBoxFor(modelItem => item.Value) <img src="@Url.Content("~/images/save.png")" class="bulk-update" data-keyword="@item.KeywordId" /></td>
    </tr>
}
@using (Html.BeginForm("EditTopic", "Admin", FormMethod.Post, new { id = "EditForm" }))
{
    @Html.Hidden("keyword")
    @Html.Hidden("replaceWith")
}

</table>

<hr />
@Html.ActionLink("Clean Keyword Counts and Remove Unused", "CleanTopics", "Admin")

</section>
@section scripts {
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/db8-ui.js")"></script>
<script>
    $("img.bulk-update").css("cursor", "pointer");
    $("img.bulk-update").click(function () {

        var keyword = $(this).attr("data-keyword")
        var replace = $(this).closest("tr").find("td:first").text();
        var replaceWith = $(this).closest("td").find("input:first").val();

        var msg = "Are you sure you want to replace '" + replace + "' with '" + replaceWith + "'?"
        if (confirm(msg)) {
            $("#keyword").val(keyword);
            $("#replaceWith").val(replaceWith);
            $("#EditForm").submit();
        }
    });
</script>
}
